package com.proj.android.canyondefense.core.impl;

import com.proj.android.canyondefense.core.base.IAudio;
import com.proj.android.canyondefense.core.base.IFileIO;
import com.proj.android.canyondefense.core.base.IGame;
import com.proj.android.canyondefense.core.base.IGraphics;
import com.proj.android.canyondefense.core.base.IInput;
import com.proj.android.canyondefense.core.base.Screen;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

public class AndroidGame extends Activity implements IGame {

	private AndroidRenderView _renderView;
	private IGraphics _graphics;
	private IAudio _audio;
	private IInput _input;
	private IFileIO _fileIO;
	private Screen _screen;
	
	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
		int frameBufferWidth = isLandscape ? 480 : 320;
		int frameBufferHeight = isLandscape ? 320 : 480;
		
		Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);
		
		Display display = getWindowManager().getDefaultDisplay();
		int displayWidth;
		int displayHeight;
		if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR2) {
			Point size = new Point();
			display.getSize(size);
			displayWidth = size.x;
			displayHeight = size.y;
		} else {
			displayWidth = display.getWidth();
			displayHeight = display.getHeight();
		}
		float scaleX = frameBufferWidth / displayWidth; 
		float scaleY = frameBufferHeight / displayHeight;
		
		_renderView = new AndroidRenderView(this, frameBuffer);
		_graphics = new AndroidGraphics(getAssets(), frameBuffer);
		_fileIO = new AndroidFileIO(getAssets());
		_audio = new AndroidAudio(this);
		_input = new AndroidInput(this, _renderView, scaleX, scaleY);
		_screen = getStartScreen();
		setContentView(_renderView);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		_screen.resume();
		_renderView.resume();
	}
	
	public IInput getInput() {
		return _input;
	}

	public IFileIO getFileIO() {
		return _fileIO;
	}

	public IAudio getAudio() {
		return _audio;
	}

	public IGraphics getGraphics() {
		return _graphics;
	}
	
	public void setScreen(Screen screen) {
		if (screen == null) {
			throw new IllegalArgumentException("[AndroidGame] :: setScreen - screen must be not null !!!");
		}
		this._screen.pause();
		this._screen.dispose();
		screen.resume();
		screen.update(0);
		this._screen = screen;
	}

	public Screen getCurrentScreen() {
		return _screen;
	}

	public Screen getStartScreen() {
		// TODO Auto-generated method stub
		return null;
	}

}
